package com.intellagric.service.sys_operate;


import com.intellagric.common.bean.LayuiDataGridResult;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.interfaces.sys_operate.OperateService;
import com.intellagric.mapper.SysOperateMapper;
import com.intellagric.mapper.SysRoleOperateMapper;
import com.intellagric.pojo.SysOperate;
import com.intellagric.pojo.SysOperateExample;
import com.intellagric.pojo.SysRoleOperateKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;



/**
 * @Auther: zhy
 * @Date: 2018/10/4
 * @Description: 菜单管理服务
 */
@Service
public class OperateServiceImpl implements OperateService {

    @Autowired
    private SysOperateMapper sysOperateMapper;
    @Autowired
    private SysRoleOperateMapper sysRoleOperateMapper;

    /**
     * 添加菜单
     * @Param sysOperate
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addOperate(SysOperate sysOperate) {

        if (sysOperateMapper.insert(sysOperate) == 1) {
            //添加成功后就在role_menu表，为超级管理员 roleid=1 添加新的menuId
            //这样超级管理员登陆后才可以管理这个新添加的菜单栏
            SysRoleOperateKey  sysRoleOperateKey=new SysRoleOperateKey();
            sysRoleOperateKey.setRoleId("1");
            sysRoleOperateKey.setOperateId(sysOperate.getId());
            if(sysRoleOperateMapper.insert(sysRoleOperateKey)==1)
                return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
        return ResponseMessage.fail();
    }

    /**
     * 根据menu的Id删除菜单
     * @Param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteOperate(String id) {
        //这里没有处理删除父类结点需要的循环删除该结点的子节点

        //先删除角色和菜单的管理表
//        SysRoleOperateExample sysRoleOperateExample=new SysRoleOperateExample();
//        sysRoleOperateExample.createCriteria().andOperateIdEqualTo(id);
//

//        sysRoleOperateMapper.countByExample(sysRoleOperateExample);

//        if(sysRoleOperateMapper.deleteByExample(sysRoleOperateExample)==1) {
//            System.out.println("sysRoleOperate删除成功");
            //在配置文件配置了切面，这里如果不成功应该会回滚把
            if (sysOperateMapper.deleteByPrimaryKey(id) == 1) {
                return ResponseMessage.success();
            } else {
                return ResponseMessage.fail();
            }
//        }

//            return ResponseMessage.fail();
    }

    /**
     * 修改菜单
     * @Param sysOperate
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editOperate(SysOperate sysOperate) {
        if (sysOperateMapper.updateByPrimaryKeySelective(sysOperate) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    @Override
    public SysOperate getOperateById(String id) {
        return sysOperateMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询菜单数据信息，生成树形菜单
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getOperateTree() {
        //查询所有列表数据
        LayuiDataGridResult result = new LayuiDataGridResult();
        List<SysOperate> menuList = this.getOperateList().getData();

        //查询顶级菜单
        List<SysOperate> rootSysOperateList = this.getOperateByParentId("0").getData();

        //生成树形菜单数据，为一级菜单设置子菜单，getChild是递归调用的
        for (SysOperate sysOperate : rootSysOperateList) {
            sysOperate.setChildren(getChild(sysOperate.getId(), menuList));
        }



        result.setData(rootSysOperateList);

        return result;
    }

    private List<SysOperate> getChild(String id, List<SysOperate> menuList) {
        // 子菜单
        List<SysOperate> childList = new ArrayList<SysOperate>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (SysOperate sysOperate : menuList) {
            if (sysOperate.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (sysOperate.getParentId().equals(id)) {
                    childList.add(sysOperate);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (SysOperate sysOperate : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            sysOperate.setChildren(getChild(sysOperate.getId(), menuList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    /**
     * 异步查询菜单，通过父id查询
     * @Param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getOperateByParentId(String parentId) {
        SysOperateExample sysOperateExample = new SysOperateExample();
        sysOperateExample.setOrderByClause("sort DESC");//以sort字段进行排序
        sysOperateExample.createCriteria().andParentIdEqualTo(parentId);
        List<SysOperate> rootSysOperateList = sysOperateMapper.selectByExample(sysOperateExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(rootSysOperateList);
        return result;
    }

    /**
     * 查询菜单列表信息
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getOperateList() {
        SysOperateExample sysOperateExample = new SysOperateExample();
        sysOperateExample.setOrderByClause("sort DESC");//以sort字段进行排序
        sysOperateExample.createCriteria().andIdIsNotNull();
        List<SysOperate> sysOperateList = sysOperateMapper.selectByExample(sysOperateExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(sysOperateList);
        return result;
    }



}
